home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / TIMER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-18  |  3.4 KB  |  183 lines

  1. /*
  2.  
  3.     timer.c
  4.  
  5.     Copyright 1994, August 23 by Alec Russell, ALL rights reserved
  6.     Permission granted to use ths code as you wish.
  7.  
  8.     Created - 1994/8/23
  9.  
  10.     Used to speed up main timer to 60 Hz
  11.  
  12.     History:
  13.         New file
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <dos.h>
  19.  
  20.  
  21. #define ULONG unsigned long
  22.  
  23. #pragma inline
  24.  
  25. volatile unsigned long fast_tick, slow_tick;
  26. static void interrupt (far *oldtimer)(void);   /* BIOS timer handler */
  27.  
  28. void deinit_timer(void);
  29.  
  30.  
  31. /*
  32.  
  33.    You don't have to call the old timer, but if you don't
  34.    you have to write some code to cleanup in de-init that
  35.    fixes DOS's internal clock.
  36.  
  37.    Its also considered 'good form' to call the old int.
  38.    If everyone does, then everything that other TSR's etc...
  39.    may have installed will also work.
  40.  
  41.  
  42.    If you skip the little chunk of ASM code- the out 20-
  43.    you WILL LOCKUP all interupts, and your computer
  44.  
  45.  
  46.    Anyways, this test replacement just increments a couple of
  47.    long ints. 
  48. */
  49. /* ---------------------- new_timer() ------------------- August 23,1994 */
  50. static void interrupt new_timer(void)
  51. {
  52.    asm cli
  53.    fast_tick++;
  54.  
  55.    if ( !(fast_tick & 3) ) // call old timer ever 4th new tick
  56.       {
  57.       oldtimer();
  58.       slow_tick++;
  59.       }
  60.    else
  61.       {
  62.       // reset PIC
  63.       asm {
  64.           mov al, 20h
  65.           out 20h, al
  66.           } 
  67.       }
  68.  
  69.    asm sti
  70. }
  71.  
  72.  
  73. /* see that 1st line of inline asm?
  74.  
  75.    to set whatever clock speed you want load
  76.    bx with 1193180/x where x is the clock speed you want in Hz.
  77.  
  78. */
  79. /* ---------------------- init_timer() ------------------ August 23,1994 */
  80. void init_timer(void)
  81. {
  82.  
  83.    slow_tick=fast_tick=0l;
  84.    oldtimer=getvect(8); // save old timer
  85.  
  86.    asm cli
  87.  
  88.    // speed up clock
  89.    asm {
  90.       mov     bx,  19886     //  set the clock speed to 60Hz (1193180/60)
  91.        mov     al,  00110110b
  92.         out     43h, al
  93.       mov     al,  bl
  94.        out     40h, al
  95.         mov     al,  bh
  96.       out     40h, al
  97.       }
  98.  
  99.    setvect(8, new_timer);
  100.  
  101.    asm sti
  102.  
  103. }
  104.  
  105.  
  106. /* ---------------------- deinit_timer() ---------------- August 23,1994 */
  107. void deinit_timer(void)
  108. {
  109.    asm cli
  110.  
  111.    // slow down clock   1193180 / 65536 = 18.2, but we use zero
  112.  
  113.    asm {
  114.       xor bx,  bx          // min rate 18.2 Hz when set to zero
  115.         mov al,  00110110b
  116.       out 43h, al
  117.        mov al,  bl
  118.         out 40h, al
  119.       mov al,  bh
  120.        out 40h, al
  121.       }
  122.  
  123.    setvect(8, oldtimer);  // restore oldtimer
  124.  
  125.    asm sti
  126.  
  127. }
  128.  
  129.  
  130. /* ---------------------- waitt() --------------------- February 11,1995 */
  131. void waitt(ULONG t)
  132. {
  133.    t+=fast_tick;
  134.  
  135.    while ( t > fast_tick )
  136.       ;
  137. }
  138.  
  139.  
  140. #define TEST 1
  141.  
  142. #if TEST
  143.  
  144. #include <stdio.h>
  145. #include <bios.h>
  146.  
  147.  
  148. /* ---------------------- get_fast() -------------------- August 23,1994 */
  149. ULONG get_fast(void)
  150. {
  151.    return(fast_tick);
  152. }
  153.  
  154. /* ---------------------- get_slow() -------------------- August 23,1994 */
  155. ULONG get_slow(void)
  156. {
  157.    return(slow_tick);
  158. }
  159.  
  160. /* ---------------------- main() ------------------------ August 23,1994 */
  161. void main(void)
  162. {
  163.    ULONG t1, t2;
  164.  
  165.    printf("init timer\n");
  166.    init_timer();
  167.  
  168.    while ( !bioskey(1) )
  169.       {
  170.       t1=get_fast();
  171.       t2=get_slow();
  172.       printf("fast %lu slow %lu\n", t1, t2);
  173.       }
  174.  
  175.    deinit_timer();
  176. }
  177.  
  178.  
  179. #endif
  180.  
  181. /* ------------------------------ EOF -------------------------------- */
  182.  
  183.